import java.util.*;
import java.text.*;

public class Patient implements ID
{
    public Patient()
    {
    	//indexCheckup = 0;
        pd = PersonalData.getPersonalData();
        a = new ArrayList<AnthropometryData>(2);
        //a.add(AnthropometryData.getAnthropometry());
        d = new ArrayList<Diagnostic>(2);
        //d.add(new Diagnostic());
    }
    public Patient(PersonalData pd_,ArrayList<AnthropometryData> a_)
    {
    	//indexCheckup = a_.size()-1;
        pd = pd_;
        a = this.chronologicalOrderA(a_);
        d = new ArrayList<Diagnostic>(a.size());
        /*for(int i = 0; i < a.size(); i++)
        	d.add(new Diagnostic());*/
    }
    public Patient(PersonalData pd_,ArrayList<AnthropometryData> a_,ArrayList<Diagnostic> d_)
    {
    	//indexCheckup = a_.size()-1;
        pd = pd_;
        a = this.chronologicalOrderA(a_);
        d = this.chronologicalOrderD(d_);
        /*if(a.size() == d_.size())
        	d = d_;
        else
        {
        	d = d_;
        	for(int i = 0; i < a.size()-d_.size(); i++)
            	d.add(new Diagnostic());
    	}*/
    }
    public void setDiagnostic(Date date_,String diagnostic_)
	{
		//d.get(indexCheckup).setDiagnostic(diagnostic_);
    	int index = this.getIndexDiagnostic(date_);
    	if(index >= 0)
    		d.get(index).setDiagnostic(diagnostic_);
    	else
    	{
    		d.add(new Diagnostic(date_,diagnostic_));
    		d = this.chronologicalOrderD(d);
    	}
	}
    public void setPersonalData(PersonalData pd_)
    {
        pd = pd_;
    }
    /*public void addDiagnostic(Diagnostic d_)
    {
    	d.add(d_);
    }
    public void addAnthropometryData(AnthropometryData a_)
    {
        a.add(a_);
    }*/
    public void setAnthropometryData(AnthropometryData a_)
    {
    	int index = this.getIndexAData(a_.getDate());
    	if(index >= 0)
    	{
    		a.remove(index);
    		a.add(a_);
    		a = this.chronologicalOrderA(a);
    	}
    	else
    	{
    		a.add(a_);
    		a = this.chronologicalOrderA(a);
    	}
    }
    public void setAnthropometryList(ArrayList<AnthropometryData> a_)
    {
        a = a_;
    }
    public String getDianostic(Date date_)
	{
    	int index = this.getIndexDiagnostic(date_);
    	if(index >= 0)
    		return d.get(index).getDianostic();
    	else
    		return null;
	}
    public ArrayList<Diagnostic> getDiagnosticList()
    {
    	return d;
    }
    /*public String getName()
    {
        return pd.getName();
    }
    public String getLastName()
    {
        return pd.getLastName();
    }
    public String getGender()
    {
        return pd.getGender();
    }
    public String getOccupation()
    {
        return pd.getOccupation();
    }
    public String getCoachsName()
    {
        return pd.getCoachsName();
    }
    public String getSport()
    {
        return pd.getSport();
    }
    public String getCategory()
    {
        return pd.getCategory();
    }
    public String getInstitution()
    {
        return pd.getInstitution();
    }
    public Date getDateBirth()
    {
        return pd.getDateBirth();
    }*/
    /*public Date getDate()
    {
        return a.get(indexCheckup).getDate();
    }*/
    /*public float getAnthropometryValue(Date date_, ID.A id)
    {
    	int index = this.getIndexAData(date_);
        return a.get(index).getAnthropometryValue(id);
    }
    public float getAnthropometryValue(Date date_, ID.A_CREASE id)
    {
    	int index = this.getIndexAData(date_);
        return a.get(index).getAnthropometryValue(id);
    }
    public float getAnthropometryValue(Date date_, ID.A_GIRTH id)
    {
    	int index = this.getIndexAData(date_);
        return a.get(index).getAnthropometryValue(id);
    }
    public float getAnthropometryValue(Date date_, ID.A_DIAMETER id)
    {
    	int index = this.getIndexAData(date_);
        return a.get(index).getAnthropometryValue(id);
    }*/
    /*public void setName(String name_)
    {
        pd.setName(name_);
    }
    public void setLastName(String lastName_)
    {
        pd.setLastName(lastName_);
    }
    public void setGender(String gender_)
    {
        pd.setGender(gender_);
    }
    public void setOccupation(String occupation_)
    {
        pd.setOccupation(occupation_);
    }
    public void setCoachsName(String coachsName_)
    {
        pd.setCoachsName(coachsName_);
    }
    public void setSport(String sport_)
    {
        pd.setSport(sport_);
    }
    public void setCategory(String category_)
    {
        pd.setCategory(category_);
    }
    public void setInstitution(String institution_)
    {
        pd.setInstitution(institution_);
    }
    public void setDateBirth(Date dateBirth_)
    {
        pd.setDateBirth(dateBirth_);
    }*/
    /*public void setDate(Date date_)
    {
    	DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, new Locale("es","MX"));
    	int index = -1;
    	for(int i = 0; i < a.size(); i++)
    		if(df.format(a.get(i).getDate()).equals(df.format(date_)))
    		{
    			indexCheckup = i;
    			return;
    		}else if(a.get(i).getDate().compareTo(date_) < 0)
    		{
				index++;
			}
    	if(index < a.size()-1)
    	{
    		index++;
    		a.add(index,AnthropometryData.getAnthropometry());
	    	d.add(index,new Diagnostic());
	    	indexCheckup = index;
	        a.get(indexCheckup).setDate(date_);
	        return;
    	}
    	a.add(AnthropometryData.getAnthropometry());
    	d.add(new Diagnostic());
    	indexCheckup = a.size()-1;
        a.get(indexCheckup).setDate(date_);
    }*/
    /*public void setAnthropometryValue(Date date_, ID.A id, float value)
    {
    	int index = this.getIndexAData(date_);
        a.get(index).setAnthropometryValue(id,value);
    }
    public void setAnthropometryValue(Date date_, ID.A_CREASE id, float value)
    {
    	int index = this.getIndexAData(date_);
        a.get(index).setAnthropometryValue(id,value);
    }
    public void setAnthropometryValue(Date date_, ID.A_GIRTH id, float value)
    {
    	int index = this.getIndexAData(date_);
        a.get(index).setAnthropometryValue(id,value);
    }
    public void setAnthropometryValue(Date date_, ID.A_DIAMETER id, float value)
    {
    	int index = this.getIndexAData(date_);
        a.get(index).setAnthropometryValue(id, value);
    }*/
    /*public void setValue(ID.PD id, String value)
    {
        pd.setValue( id, value);
    }*/
    /*public void setValue(ID.OTHER id,String value)
    {
    	switch(id)
        {
	        case DATE:
	            try{
	                DateFormat df = 
	                    DateFormat.getDateInstance(
	                        DateFormat.SHORT, 
	                        new Locale("es","MX")
	                    );
	                setDate(df.parse(value));
	            }catch(ParseException e){e.printStackTrace();}
	            break;
	        case DIAGNOSTIC:
	        	this.setDiagnostic(value);
	        	break;
        }
    }*/
    /*public void setValue(Date date_, ID.A id, String value)
    {
    	int index = this.getIndexAData(date_);
        a.get(index).setAnthropometryValue(id,value);
    }
    public void setValue(Date date_, ID.A_CREASE id, String value)
    {
    	int index = this.getIndexAData(date_);
        a.get(index).setAnthropometryValue(id,value);
    }
    public void setValue(Date date_, ID.A_GIRTH id, String value)
    {
    	int index = this.getIndexAData(date_);
        a.get(index).setAnthropometryValue(id,value);
    }
    public void setValue(Date date_, ID.A_DIAMETER id,String value)
    {
    	int index = this.getIndexAData(date_);
        a.get(index).setAnthropometryValue(id,value);
    }*/
    /*public void setIndexCheckup(int index)
    {
    	indexCheckup = index;
    }*/
    public PersonalData getPersonalData()
    {
        return pd;
    }
    public ArrayList<AnthropometryData> getAnthropometryList()
    {
        return a;
    }
    public Date[] getDateList()
    {
    	if(a.isEmpty())
    		return null;
    	Date[] dateList = new Date[a.size()];
    	for(int i = 0; i < a.size(); i++)
    		dateList[i] = a.get(i).getDate();
    	return dateList;
    }
    public AnthropometryData getAnthropometryData(Date date_)
    {
    	int index = this.getIndexAData(date_);
    	if(index >= 0)
    		return a.get(index);
    	else
    		return null;
    }
    /*public AnthropometryData getAnthropometryData(int index)
    {
        return a.get(index);
    }*/
    public boolean deleteDate(Date date_)
    {
    	//DateFormat df = Resources.getDateFormat();
    	int index_A = this.getIndexAData(date_);
    	int index_D = this.getIndexDiagnostic(date_);
    	if(index_A >= 0)
    	{
    		a.remove(index_A);
    		if(index_D >=0)
    			d.remove(index_D);
    		return true;
    	}
    	else
    		return false;
    	/*for(int i = 0; i < a.size(); i++)
    		if(df.format(a.get(i).getDate()).equals(df.format(date_)))
    		{
    			//if(indexCheckup == i)
    				if(i == 0)
    					indexCheckup = 0;
    				else //if(i == a.size()-1)
    					indexCheckup = i-1;
    				//else 
    					//indexCheckup = i+1;
    			a.remove(i);
    			if(a.isEmpty())
    				a.add(AnthropometryData.getAnthropometry());
    			return true;
    		}
    	return false;*/
    }
    /*public Date[] getListCheckups()
    {
    	Date[] date = new Date[a.size()];
    	for(int i = 0; i < a.size(); i++)
    		date[i] = a.get(i).getDate();
    	return date;
    }*/
    /*public int getIndexCheckup()
    {
    	return indexCheckup;
    }*/
    public int getTotal()
    {
    	return a.size();
    }
    private ArrayList<AnthropometryData> chronologicalOrderA(ArrayList<AnthropometryData> a_)
    {
    	DateFormat df = Resources.getDateFormat();
    	AnthropometryData adata_aux;
    	ArrayList<AnthropometryData> a_aux = new ArrayList<AnthropometryData> (a_.size());
    	int j = 0;
    	for(int i = 0; i < a_.size(); i++)
    	{
    		j = 0;
    		adata_aux = a_.get(i);
    		if(i != 0)
    			while(df.format(adata_aux.getDate()).compareTo(df.format(a_aux.get(j).getDate())) < 0)
    			{
    				j++;
    				if(j >= a_aux.size())
    					break;
    			}
    		a_aux.add(j, adata_aux);
    	}
    	return a_aux;
    }
    private ArrayList<Diagnostic> chronologicalOrderD(ArrayList<Diagnostic> d_)
    {
    	DateFormat df = Resources.getDateFormat();
    	Diagnostic diag_aux;
    	ArrayList<Diagnostic> d_aux = new ArrayList<Diagnostic> (d_.size());
    	int j = 0;
    	for(int i = 0; i < d_.size(); i++)
    	{
    		j = 0;
    		diag_aux = d_.get(i);
    		if(i != 0)
    			while(df.format(diag_aux.getDate()).compareTo(df.format(d_aux.get(j).getDate())) < 0)
    			{
    				j++;
    				if(j >= d_aux.size())
    					break;
    			}
    		d_aux.add(j, diag_aux);
    	}
    	return d_aux;
    }
    private int getIndexDiagnostic(Date date_)
    {
    	DateFormat df = Resources.getDateFormat();
    	for(int i = 0; i < d.size(); i++)
    		if(df.format(date_).equals(df.format(d.get(i).getDate())))
    			return i;
    	return -1;
    }
    private int getIndexAData(Date date_)
    {
    	DateFormat df = Resources.getDateFormat();
    	for(int i = 0; i < a.size(); i++)
    		if(df.format(date_).equals(df.format(a.get(i).getDate())))
    			return i;
    	return -1;
    }
    private boolean dateExist(Date date_)
    {
    	DateFormat df = Resources.getDateFormat();
    	for(int i = 0; i < a.size(); i++)
    		if(df.format(date_).equals(df.format(a.get(i).getDate())))
    			return true;
    	return false;
    }
    //private int indexCheckup;
    private PersonalData pd;
    private ArrayList<AnthropometryData> a;
    private ArrayList<Diagnostic> d;
}
